package com.tianyang.yukuaifu.business.main.presenter;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.tianyang.yukuaifu.business.auth.model.AuthModel;
import com.tianyang.yukuaifu.business.auth.view.AuthCompanyActivity;
import com.tianyang.yukuaifu.business.auth.view.AuthIndividualActivity;
import com.tianyang.yukuaifu.business.auth.view.AuthMerchantActivity;
import com.tianyang.yukuaifu.business.main.bean.ChannelOutput;
import com.tianyang.yukuaifu.business.main.bean.PosRateOutput;
import com.tianyang.yukuaifu.business.main.contract.RateConfigContract;
import com.tianyang.yukuaifu.business.main.model.MainModel;
import com.tianyang.yukuaifu.business.main.view.RateConfigsActivity;
import com.tianyang.yukuaifu.jump.eventbus.RefreshUserEvent;
import com.tianyang.yukuaifu.module.dialog.HintDialog;
import com.tianyang.yukuaifu.module.dialog.interfaces.OnBtnClickL;
import com.tianyang.yukuaifu.utils.AppContextUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.List;
import java.util.Map;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：费率配置
 * Created by xyj on 2024/3/10
 */
public class RateConfigPresenter implements RateConfigContract.Presenter {

    private RateConfigContract.View mView;

    private CompositeSubscription mSubscription;

    private String userName, params;

    private int type;

    public RateConfigPresenter(RateConfigContract.View mView, String userName, int type, String params) {
        this.mView = mView;
        this.userName = userName;
        this.type = type;
        this.params = params;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void getChannelList() {
        mView.showLoadingDialog(false);
        Subscription subscription = MainModel.getInstance().getChannelList()
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<ChannelOutput>>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(List<ChannelOutput> outputs) {
                        if (outputs != null && !outputs.isEmpty()) {
                            getPosTemplateRate(outputs.get(0).getCode());
                            mView.showChannelView(outputs);
                        }
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void getPosTemplateRate(String posChannel) {
        mView.showLoadingDialog(false);
        Subscription subscription = MainModel.getInstance().getPosTemplateRate(userName, type, posChannel)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<PosRateOutput>>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(List<PosRateOutput> outputs) {
                        mView.showPosTemplateRateView(outputs);
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void addPosUserRate(List<PosRateOutput> outputs) {
        mView.showLoadingDialog(false);
        Subscription subscription = MainModel.getInstance().addPosUserRate(outputs, userName, type)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<JSONObject>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(JSONObject object) {
                        if (!TextUtils.isEmpty(params)) {
                            Map<String, String> map = JSON.parseObject(params, new TypeReference<Map<String, String>>() {});
                            sendIdCert(map);
                        } else {
                            new HintDialog(mView.getContext())
                                    .title("温馨提示")
                                    .content("费率配置修改成功")
                                    .show();
                        }
                    }
                });
        mSubscription.add(subscription);
    }

    private void sendIdCert(Map<String, String> map) {
        Subscription subscription = AuthModel.getInstance().sendIdCert(map)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Object>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(Object obj) {
                        new HintDialog(mView.getContext())
                                .title("上传成功")
                                .content("提交成功，请等待审核!")
                                .listener(new OnBtnClickL() {
                                    @Override
                                    public void onBtnClick() {
                                        EventBus.getDefault().post(new RefreshUserEvent());
                                        AppContextUtil.finishActivity(AuthCompanyActivity.class.getName(),
                                                AuthIndividualActivity.class.getName(), AuthMerchantActivity.class.getName(),
                                                RateConfigsActivity.class.getName());
                                    }
                                }).show();
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
